{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 讯飞语音合成TTS+Moivepy=自动合成视频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "from moviepy.editor import AudioFileClip,ImageClip,ImageSequenceClip,CompositeVideoClip,TextClip,concatenate_videoclips\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄鹤楼送孟浩然之广陵', '唐代:李白', '故人西辞黄鹤楼，', '烟花三月下扬州。', '孤帆远影碧空尽，', '唯见长江天际流。']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#https://so.gushiwen.org/search.aspx?value=%E9%BB%84%E9%B9%A4%E6%A5%BC\n",
    "txt='''黄鹤楼送孟浩然之广陵\n",
    "唐代:李白\n",
    "故人西辞黄鹤楼，\n",
    "烟花三月下扬州。\n",
    "孤帆远影碧空尽，\n",
    "唯见长江天际流。'''\n",
    "tl=txt.split('\\n')\n",
    "tl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xf_service import tts_say_wave"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "黄鹤楼送孟浩然之广陵\n",
      "{'common': {'app_id': '4f979d67'}, 'business': {'aue': 'raw', 'auf': 'audio/L16;rate=16000', 'vcn': 'xiaoxue', 'tte': 'utf8', 'ent': 'aisound'}, 'data': {'status': 2, 'text': '6buE6bmk5qW86YCB5a2f5rWp54S25LmL5bm/6Zm1'}}\n",
      "------------------------------\n",
      "接收信息\n",
      "ws is closed\n",
      "写入  wav/poem-0.wav\n",
      "唐代:李白\n",
      "{'common': {'app_id': '4f979d67'}, 'business': {'aue': 'raw', 'auf': 'audio/L16;rate=16000', 'vcn': 'xiaoxue', 'tte': 'utf8', 'ent': 'aisound'}, 'data': {'status': 2, 'text': '5ZSQ5LujOuadjueZvQ=='}}\n",
      "------------------------------\n",
      "接收信息\n",
      "ws is closed\n",
      "写入  wav/poem-1.wav\n",
      "故人西辞黄鹤楼，\n",
      "{'common': {'app_id': '4f979d67'}, 'business': {'aue': 'raw', 'auf': 'audio/L16;rate=16000', 'vcn': 'xiaoxue', 'tte': 'utf8', 'ent': 'aisound'}, 'data': {'status': 2, 'text': '5pWF5Lq66KW/6L6e6buE6bmk5qW877yM'}}\n",
      "------------------------------\n",
      "接收信息\n",
      "ws is closed\n",
      "写入  wav/poem-2.wav\n",
      "烟花三月下扬州。\n",
      "{'common': {'app_id': '4f979d67'}, 'business': {'aue': 'raw', 'auf': 'audio/L16;rate=16000', 'vcn': 'xiaoxue', 'tte': 'utf8', 'ent': 'aisound'}, 'data': {'status': 2, 'text': '54Of6Iqx5LiJ5pyI5LiL5oms5bee44CC'}}\n",
      "------------------------------\n",
      "接收信息\n",
      "ws is closed\n",
      "写入  wav/poem-3.wav\n",
      "孤帆远影碧空尽，\n",
      "{'common': {'app_id': '4f979d67'}, 'business': {'aue': 'raw', 'auf': 'audio/L16;rate=16000', 'vcn': 'xiaoxue', 'tte': 'utf8', 'ent': 'aisound'}, 'data': {'status': 2, 'text': '5a2k5biG6L+c5b2x56Kn56m65bC977yM'}}\n",
      "------------------------------\n",
      "接收信息\n",
      "ws is closed\n",
      "写入  wav/poem-4.wav\n",
      "唯见长江天际流。\n",
      "{'common': {'app_id': '4f979d67'}, 'business': {'aue': 'raw', 'auf': 'audio/L16;rate=16000', 'vcn': 'xiaoxue', 'tte': 'utf8', 'ent': 'aisound'}, 'data': {'status': 2, 'text': '5ZSv6KeB6ZW/5rGf5aSp6ZmF5rWB44CC'}}\n",
      "------------------------------\n",
      "接收信息\n",
      "ws is closed\n",
      "写入  wav/poem-5.wav\n",
      "[('wav/poem-0.wav', '黄鹤楼送孟浩然之广陵'), ('wav/poem-1.wav', '唐代:李白'), ('wav/poem-2.wav', '故人西辞黄鹤楼，'), ('wav/poem-3.wav', '烟花三月下扬州。'), ('wav/poem-4.wav', '孤帆远影碧空尽，'), ('wav/poem-5.wav', '唯见长江天际流。')]\n"
     ]
    }
   ],
   "source": [
    "#先TTS，生成语音文件\n",
    "poem_wav=[]\n",
    "for index,sens in enumerate(tl):\n",
    "    fp=f'wav/poem-{index}.wav'\n",
    "    tts_say_wave(fp,sens)\n",
    "    poem_wav.append((fp,sens))\n",
    "print(poem_wav)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1.图片轮播\n",
    "2.音频合并，追加\n",
    "3.图片，音频合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['images/242.jpg', 'images/241.jpg', 'images/243.jpg', 'images/240.jpg']\n",
      "['images/240.jpg', 'images/241.jpg', 'images/242.jpg', 'images/243.jpg']\n"
     ]
    }
   ],
   "source": [
    "images=[f'images/{img}' for img in os.listdir('images') if img.endswith('jpg') ]\n",
    "print(images)\n",
    "images=sorted(images)\n",
    "print(images)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 4)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(tl),len(images)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "font='Microsoft-YaHei'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wav/poem-0.wav 黄鹤楼送孟浩然之广陵\n",
      "wav/poem-1.wav 唐代:李白\n",
      "wav/poem-2.wav 故人西辞黄鹤楼，\n",
      "wav/poem-3.wav 烟花三月下扬州。\n",
      "wav/poem-4.wav 孤帆远影碧空尽，\n",
      "wav/poem-5.wav 唯见长江天际流。\n"
     ]
    }
   ],
   "source": [
    "acList=[]\n",
    "for index,(fp,title) in enumerate(poem_wav):\n",
    "    print(fp,title)\n",
    "    #图片+字幕+声音\n",
    "    tc=TextClip(title, font=font, fontsize=30, color='white').set_pos(('center','bottom'))\n",
    "    ac=AudioFileClip(fp)\n",
    "    #每隔几个声音再更换相片\n",
    "    im=ImageClip(images[index%len(images)]).resize(height=576,width=720)\n",
    "    im2=im.set_audio(ac).set_duration(ac.duration)\n",
    "    cv=CompositeVideoClip([im2,tc])\n",
    "    cv2=cv.set_duration(im2.duration).resize(height=576,width=720)\n",
    "    \n",
    "    acList.append(cv2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<moviepy.video.compositing.CompositeVideoClip.CompositeVideoClip at 0x7f62848e16d8>,\n",
       " <moviepy.video.compositing.CompositeVideoClip.CompositeVideoClip at 0x7f6284688198>,\n",
       " <moviepy.video.compositing.CompositeVideoClip.CompositeVideoClip at 0x7f628430dd30>,\n",
       " <moviepy.video.compositing.CompositeVideoClip.CompositeVideoClip at 0x7f62841746a0>,\n",
       " <moviepy.video.compositing.CompositeVideoClip.CompositeVideoClip at 0x7f62842f28d0>,\n",
       " <moviepy.video.compositing.CompositeVideoClip.CompositeVideoClip at 0x7f6284b1ef98>]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "chunk:  12%|█▏        | 32/278 [00:00<00:00, 318.93it/s, now=None]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Moviepy - Building video hhl.mp4.\n",
      "MoviePy - Writing audio in %s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "t:   1%|          | 3/302 [00:00<00:10, 27.47it/s, now=None]       "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MoviePy - Done.\n",
      "Moviepy - Writing video hhl.mp4\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                              \r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Moviepy - Done !\n",
      "Moviepy - video ready hhl.mp4\n"
     ]
    }
   ],
   "source": [
    "#合成视频\n",
    "final_video= concatenate_videoclips(acList,method=\"compose\")\n",
    "final_video.resize(height=576,width=720).write_videofile(\"hhl.mp4\",fps=24)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
